<?php

namespace Dcat\Admin\ApiTester\Http\Controllers;

use Dcat\Admin\ApiTester\Repositories\CgkjProj;
use Dcat\Admin\ApiTester\Models\CgkjProj as CgkjProjModels;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Http\Controllers\Controller;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\ApiTester\Actions\SwitchGridView;
use Illuminate\Http\Request;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Form\NestedForm;

class CgkjProjController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->title('项目管理')
            ->description('')
            ->breadcrumb(['text' => '项目管理', 'url' => ''])
            ->body($this->grid());
    }

    /**
     * @desc
     * author eRic
     * dateTime 2022-12-31 10:40
     */
    public function grid(){
        return Grid::make(new CgkjProj(), function (Grid $grid) {
            if (request()->get('_view_') !== 'list') {
                // 设置自定义视图
                $grid->view('ycookies.api-tester::subview.projbox');
                $grid->actions(function ($actions){
                    $actions->disableDelete();
                });
                //$grid->setActionClass(Grid\Displayers\Actions::class);
            }

            $grid->tools([
                //$this->buildPreviewButton('btn-primary'),
                //new SwitchGridView(),
            ]);

            //$grid->disableCreateButton();

            $grid->column('user_id');
            $grid->column('code');
            $grid->column('name');
            $grid->column('description');
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid_old()
    {
        return Grid::make(new CgkjProj(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('code');
            $grid->column('name');
            $grid->column('description');
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CgkjProj(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('code');
            $show->field('name');
            $show->field('description');
            $show->field('host','项目接口域名');
            $show->field('basePath','项目接口根目录');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $type = Request()->get('type','');
        if($type == 'common_param'){
            return Form::make(new CgkjProj(), function (Form $form) {
                $form->table('common_param','公共参数',function (NestedForm $table) {
                    $table->text('field_name', '字段名')->prepend('')->required();
                    $table->select('field_type', '数据类型')->options(['string' => 'string', 'integer' => 'integer', 'number' => 'number', 'boolean' => 'boolean', 'file' => 'file'])->required();
                    $table->radio('field_required', '是否必值')->options(['Y' => '是', 'N' => '否'])->required();
                    $table->text('field_default', '默认值')->prepend('');
                    $table->text('field_value', '测试值')->prepend('');
                    $table->text('field_desc', '字段描述')->prepend('');
                });
            });
        }

        if($type == 'header_param'){
            return Form::make(new CgkjProj(), function (Form $form) {
                $form->table('head_param','header 参数',function (NestedForm $table) {
                    $table->text('field_name', '字段名')->prepend('')->required();
                    $table->select('field_type', '数据类型')->options(['string' => 'string', 'integer' => 'integer', 'number' => 'number', 'boolean' => 'boolean', 'file' => 'file'])->required();
                    $table->radio('field_required', '是否必值')->options(['Y' => '是', 'N' => '否'])->required();
                    $table->text('field_default', '默认值')->prepend('');
                    $table->text('field_value', '测试值')->prepend('');
                    $table->text('field_desc', '字段描述')->prepend('');
                });
            });
        }

        return Form::make(new CgkjProj(), function (Form $form) {
            $form->display('id');
            $form->image('logo','项目图标')->width(3)->default(asset('vendor/dcat-admin-extensions/ycookies/api-tester/img/proj_logo.jpg'));
            $form->hidden('user_id')->value(Admin::guard()->user()->id);
            $form->text('code','项目编号')->help('英文或数字,用于接口文档json文件名称');
            $form->text('name','项目名称')->help('中英文数字都可');
            $form->text('host','项目接口域名')->help('如：https://demo.saishiyun.net/');
            $form->text('basePath','项目接口根目录')->help('如：api');
            $form->table('common_param','公共参数',function (NestedForm $table) {
                $table->text('field_name', '字段名')->prepend('')->required();
                $table->select('field_type', '数据类型')->options(['string' => 'string', 'integer' => 'integer', 'number' => 'number', 'boolean' => 'boolean', 'file' => 'file'])->required();
                $table->radio('field_required', '是否必值')->options(['Y' => '是', 'N' => '否'])->required();
                $table->text('field_default', '默认值')->prepend('');
                $table->text('field_value', '测试值')->prepend('');
                $table->text('field_desc', '字段描述')->prepend('');
            });
            $form->table('head_param','header 参数',function (NestedForm $table) {
                $table->text('field_name', '字段名')->prepend('')->required();
                $table->select('field_type', '数据类型')->options(['string' => 'string', 'integer' => 'integer', 'number' => 'number', 'boolean' => 'boolean', 'file' => 'file'])->required();
                $table->radio('field_required', '是否必值')->options(['Y' => '是', 'N' => '否'])->required();
                $table->text('field_default', '默认值')->prepend('');
                $table->text('field_value', '测试值')->prepend('');
                $table->text('field_desc', '字段描述')->prepend('');
            });
            $form->editor('description','项目描述');
            $form->switch('status','状态');
            $form->display('created_at');
            //$form->display('updated_at');
        });
    }

    // 获取接口文档
    public function jsonBuild(Request $request){
        $docId = $request->get('docId');
        if(empty($docId)){
            return JsonResponse::make()->error('文档ID不能为空')->refresh();
        }
        ///Users/yangg/Downloads/www/dcat/public/vendor/dcat-admin-extensions/ycookies/api-tester/json/CaseOpen.json
        $jsonfile = public_path('vendor/dcat-admin-extensions/ycookies/api-tester/json/'.$docId.'.json');
        if(!file_exists($jsonfile)){
            return JsonResponse::make()->error('未找到文档');
        }
        $json_content = file_get_contents($jsonfile);
        if(empty($json_content)){
            return JsonResponse::make()->error('文档内容异常');
        }

        header('Content-Type: application/json');
        echo $json_content;
        exit;
    }
    /**
     * @desc 展示文档
     * @param Request $request
     * author eRic
     * dateTime 2022-12-31 20:39
     */
    public function apidoc(Request $request){
        $proj_code = $request->get('docId','');
        $json_path = public_path('vendor/dcat-admin-extensions/ycookies/api-tester/json/'.$proj_code.'.json');
        if(!file_exists($json_path)){
            abort('404','文档还没有生成，请联系文档管理员');
        }
        $json_content = file_get_contents($json_path);
        if(empty($json_content)){
            abort('404','文档内容异常，请联系文档管理员');
        }
        $projinfo = CgkjProjModels::where(['code'=> $proj_code])->first();
        $apiBaseUrl = $projinfo->host.$projinfo->basePath;
        $data['apiBaseUrl'] = $apiBaseUrl;
        $data['proj_name']=  $projinfo->name;
        return view('ycookies.api-tester::apidoc.index',$data);
    }
}
